package person.aoao.library.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import person.aoao.library.dao.BookListDao;
import person.aoao.library.dao.CommentListDao;
import person.aoao.library.dao.UserListDao;
import person.aoao.library.entity.CommentList;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.CommentListService;
import person.aoao.library.vo.ListData;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CommentList)表服务实现类
 *
 * @author makejava
 * @since 2023-05-05 10:15:41
 */
@Service("commentListService")
public class CommentListServiceImpl implements CommentListService {
    @Resource
    private CommentListDao commentListDao;

    @Resource
    private UserListDao userListDao;

    @Resource
    private BookListDao bookListDao;
    /**
     * 通过ID查询单条数据
     *
     * @param cid 主键
     * @return 实例对象
     */
    @Override
    public CommentList queryById(Integer cid) {
        return this.commentListDao.queryById(cid);
    }

    /**
     * 分页查询
     *
     * @param commentList 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<CommentList> queryByPage(CommentList commentList, PageRequest pageRequest) {
        long total = this.commentListDao.count(commentList);
        return new PageImpl<>(this.commentListDao.queryAllByLimit(commentList, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param commentList 实例对象
     * @return 实例对象
     */
    @Override
    public CommentList insert(CommentList commentList) {
        this.commentListDao.insert(commentList);
        return commentList;
    }

    /**
     * 修改数据
     *
     * @param commentList 实例对象
     * @return 实例对象
     */
    @Override
    public CommentList update(CommentList commentList) {
        this.commentListDao.update(commentList);
        return this.queryById(commentList.getCid());
    }

    /**
     * 通过主键删除数据
     *
     * @param cid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer cid) {
        return this.commentListDao.deleteById(cid) > 0;
    }

    @Override
    public ResponseData getComments(Integer bookid, Integer num, Integer page) {
        ListData data=new ListData();
        List<CommentList> list= this.commentListDao.getCommentsByBookid(bookid,num,(page-1)*num);
        for(CommentList item:list){
            item.setUserList(this.userListDao.queryByUid(item.getUid()));
        }
        long count=this.commentListDao.getCountByBook(bookid);
        data.setData(list);
        data.setCount(count);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }

    @Override
    public ResponseData getCommentsByUid(String uid, Integer num, Integer page) {
        ListData data=new ListData();
        List<CommentList> list= this.commentListDao.getCommentsByUid(uid,num,(page-1)*num);
        for(CommentList item:list){
            item.setBookList(this.bookListDao.queryById(item.getBookid()));
            item.setUserList(this.userListDao.queryByUid(item.getUid()));
        }
        long count=this.commentListDao.getCountByUid(uid);
        data.setData(list);
        data.setCount(count);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }
}
